/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.tsunami.plugins.detectors.directorytraversal.genericpathtraversaldetector;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.tsunami.common.net.http.HttpRequest;
import com.google.tsunami.proto.NetworkService;

/**
 * An {@code ExploitGenerator} generates {@code PotentialExploits} by leveraging the {@code
 * InjectionPoint} API.
 */
final class ExploitGenerator {
  private final HttpRequest request;

  private final ImmutableSet<InjectionPoint> injectionPoints;

  private final NetworkService networkService;

  ExploitGenerator(
      HttpRequest request,
      NetworkService networkService,
      ImmutableSet<InjectionPoint> injectionPoints) {
    this.request = request;
    this.networkService = networkService;
    this.injectionPoints = injectionPoints;
  }

  ImmutableList<PotentialExploit> injectPayload(String payload) {
    ImmutableList.Builder<PotentialExploit> potentialExploits = ImmutableList.builder();
    for (InjectionPoint injectionPoint : this.injectionPoints) {
      potentialExploits.addAll(
          injectionPoint.injectPayload(this.networkService, this.request, payload));
    }
    return potentialExploits.build();
  }

  @Override
  public final String toString() {
    return String.format("ExploitGenerator{request=%s}", this.request);
  }
}
